//
//  AXFMyViewController.m
//  AXFEightGroup
//
//  Created by ooops on 12/22/16.
//  Copyright © 2016 ya. All rights reserved.
//

#import "AXFMyViewController.h"
#import "AXFHHeadView.h"
#import "AXFTableHeadView.h"
#import "AXFConponViewController.h"
#import <UMSocialCore/UMSocialCore.h>
#import <UShareUI/UShareUI.h>
#import "AXFSettingController.h"
#import "AXFMessageController.h"
#import "AXFMyOredrController.h"

static NSString *cell_id = @"my_cell";

@interface AXFMyViewController ()<UITableViewDelegate, UITableViewDataSource,AXFTableHeadViewDelegate>

@property (nonatomic, strong) NSArray *myArray;
@property (nonatomic, strong) AXFHHeadView *headView;
@property (nonatomic, weak) UITableView *tableView;

@end

@implementation AXFMyViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupHeadView];
    [self setupTableView];
    [self setupTableHeadView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}

#pragma mark - 加载数据 -
- (NSArray *)myArray {
    if (!_myArray) {
        
        ///
        [_ModelMgr mine:^(NSArray<NSArray *> * _Nonnull mineModels) {
            _myArray = mineModels;
        }];
    }
    return _myArray;
}

#pragma mark - 设置顶部视图 -
- (void)setupHeadView {
    
    _headView = [[AXFHHeadView alloc] init];
    [_headView addTarget:self action:@selector(clickHeadView:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_headView];
    
    [_headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.view);
        make.height.mas_equalTo(160);
    }];
}

- (void)clickHeadView:(AXFHHeadView *)headView {
   UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"AXFSettingController" bundle:nil];
    UITableViewController *vc = [storyBoard instantiateInitialViewController];

    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 设置tableView -
- (void)setupTableView{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];

    
    tableView.delegate = self;
    tableView.dataSource = self;
    
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cell_id];
    
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_headView.mas_bottom);
        make.leading.bottom.trailing.equalTo(self.view);
    }];
    
    _tableView = tableView;
}

#pragma mark - 设置tableViewHeadView -
- (void)setupTableHeadView {
    AXFTableHeadView *tabelHeadView = [[AXFTableHeadView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 70)];
    tabelHeadView.backgroundColor = [UIColor whiteColor];
    tabelHeadView.delegate = self;
    
    _tableView.tableHeaderView = tabelHeadView;
    
}

#pragma mark - UITableViewDataSource -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.myArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *array = self.myArray[section];
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id forIndexPath:indexPath];
    
    NSArray *array = self.myArray[indexPath.section];
    
    ///
    AXFDataModel *model = array[indexPath.row];
    
    cell.imageView.image = [UIImage imageNamed:model.iconName];
    cell.textLabel.text = model.title;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}

#pragma mark - UITableViewDelegate -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    AXFDataModel *model = self.myArray[indexPath.section][indexPath.row];
    NSString *vcStr = model.controller;
    Class class = NSClassFromString(vcStr);
    
    if (class == nil) {
        return;
    }
    
    UIViewController *vc = [[class alloc] init];
    
    if ((indexPath.section == 2 && indexPath.row == 0) || (indexPath.section == 0 && indexPath.row == 1)) {
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:vcStr bundle:nil];
        vc = [storyBoard instantiateInitialViewController];
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        
        if (indexPath.section == 1 && indexPath.row == 0) {
            //显示分享面板
            [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
                // 根据获取的platformType确定所选平台进行下一步操作
                [self shareTextToPlatformType:platformType];
            }];
            
        } else {
            vc = (indexPath.section == 0 && indexPath.row == 0) ? [[class alloc] init] : [[class alloc] initWithTitle:model.title];
             [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

#pragma mark - 实现AXFTableHeadViewDelegate方法
//实现协议方法
- (void)pushToController:(UIButton *)button{
    switch (button.tag) {
        case MineTopViewButtonTypeOrder:
            NSLog(@"我的订单");
            [self.navigationController pushViewController:[AXFMyOredrController new] animated:YES];
            break;
        case MineTopViewButtonTypeCard:
            [self.navigationController pushViewController:[AXFConponViewController new] animated:YES];
            break;
        case MineTopViewButtonTypeMessage:
            NSLog(@"消息");
            [self.navigationController pushViewController:[AXFMessageController new] animated:YES];
            break;
            
        default:
            break;
    }
}

#pragma mark - 友盟分享 -
- (void)getUserInfoForPlatform:(UMSocialPlatformType)platformType
{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType currentViewController:self completion:^(id result, NSError *error) {
        UMSocialUserInfoResponse *userinfo =result;
        NSString *message = [NSString stringWithFormat:@"name: %@\n icon: %@\n gender: %@\n",userinfo.name,userinfo.iconurl,userinfo.gender];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UserInfo"
                                                        message:message
                                                       delegate:nil
                                              cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                              otherButtonTitles:nil];
        [alert show];
    }];
}

// 分享文本
- (void)shareTextToPlatformType:(UMSocialPlatformType)platformType
{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    //设置文本
    messageObject.text = @"社会化组件UShare将各大社交平台接入您的应用，快速武装App。";
    
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            NSLog(@"************Share fail with error %@*********",error);
        }else{
            NSLog(@"response data is %@",data);
        }
    }];
}

@end
